package test.yzx.pproject.util;

import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

public class ListViewUtil {

    public interface RangeCallback{
        void onChange(int length);
    }


    private int targetItemHeight = -1;
    public void bindTargetItemScrollToHideRange(final ListView lv, final int position, final AbsListView.OnScrollListener scrollListener, final RangeCallback callback){
        lv.setOnScrollListener(new AbsListView.OnScrollListener() {
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if(scrollListener != null) scrollListener.onScrollStateChanged(view, scrollState);
            }
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if(scrollListener != null) scrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
                if(firstVisibleItem > position){
                    callback.onChange(targetItemHeight);
                    return ;
                }
                if(firstVisibleItem < position){
                    callback.onChange(0);
                    return ;
                }
                View targetChildView = lv.getChildAt(0);
                if(targetChildView==null) return ;
                if(targetItemHeight == -1) targetItemHeight = targetChildView.getHeight();
                callback.onChange(-targetChildView.getTop());
            }
        });
    }

}
